﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OptionsPageControl.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the OptionsPageControl type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TaskbarButtons
{
  #region Namespace Imports

  using System;
  using System.Diagnostics.CodeAnalysis;
  using System.Linq;
  using System.Windows.Forms;

  #endregion


  /// <summary>
  /// Describes and manipulates the options page control.
  /// </summary>
  public partial class OptionsPageControl : UserControl
  {
    #region Constants and Fields

    private OptionsPage _optionsPage;
    private TaskbarButton _selectedButton;

    #endregion


    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="OptionsPageControl"/> class.
    /// </summary>
    public OptionsPageControl()
    {
      InitializeComponent();
    }

    #endregion


    #region Properties

    /// <summary>
    /// Gets or sets the options page.
    /// </summary>
    /// <value>
    /// The options page.
    /// </value>
    public OptionsPage OptionsPage
    {
      get
      {
        return _optionsPage;
      }

      set
      {
        _optionsPage = value;

        InitializeControls();
      }
    }

    /// <summary>
    /// Gets or sets the selected button.
    /// </summary>
    /// <value>
    /// The selected button.
    /// </value>
    private TaskbarButton SelectedButton
    {
      get
      {
        return _selectedButton;
      }

      set
      {
        _selectedButton = value;

        UpdateButtonOptionsControls();
      }
    }

    #endregion


    #region Public Methods

    /// <summary>
    /// Applies the changes.
    /// </summary>
    public void ApplyChanges()
    {
      var availableButtons = _availableButtonsListBox.Items.Cast<TaskbarButton>().ToList();
      var taskbarButtons = _taskbarButtonsListBox.Items.Cast<TaskbarButton>().ToList();

      var configuration = new ButtonsConfiguration(availableButtons, taskbarButtons);

      OptionsPage.ButtonsConfiguration = configuration;
    }

    #endregion


    #region Methods

    private static void UpdateListItems(ListBox listBox)
    {
      var buttons = listBox.Items.Cast<TaskbarButton>().ToList();
      listBox.Items.Clear();

      foreach (TaskbarButton button in buttons)
      {
        listBox.Items.Add(button);
      }
    }


    private void AddButtonClick(object sender, EventArgs e)
    {
      var commandBarButton = OptionsPage.ApplicationObject.GetCommandBarButton(_menuItemPathTextBox.Text);

      if (commandBarButton == null)
      {
        MessageBoxHelpers.ShowError(Handle, "Menu item not found.", "Error");
      }
      else
      {
        var button = new TaskbarButton(OptionsPage.ApplicationObject, _menuItemPathTextBox.Text)
                       {
                          HideWhenDisabled = _hideWhenDisabledCheckBox.Checked 
                       };

        _availableButtonsListBox.Items.Add(button);
      }
    }


    private void AvailableButtonsListBoxSelectedIndexChanged(object sender, EventArgs e)
    {
      _taskbarButtonsListBox.SelectedIndexChanged -= TaskbarButtonsListBoxSelectedIndexChanged;
      _taskbarButtonsListBox.ClearSelected();
      _taskbarButtonsListBox.SelectedIndexChanged += TaskbarButtonsListBoxSelectedIndexChanged;

      SelectedButton = (TaskbarButton)_availableButtonsListBox.SelectedItem;
    }


    private void AvailablebuttonsListBoxKeyDown(object sender, KeyEventArgs e)
    {
      var selectedButton = SelectedButton;

      if (selectedButton == null || !_availableButtonsListBox.Items.Contains(SelectedButton))
      {
        return;
      }

      if (e.KeyCode == Keys.Right)
      {
        _availableButtonsListBox.Items.Remove(selectedButton);
        _taskbarButtonsListBox.Items.Add(selectedButton);
        _taskbarButtonsListBox.SelectedIndex = _taskbarButtonsListBox.Items.IndexOf(selectedButton);
      }
    }


    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Don't care.")]
    private void InitializeControls()
    {
      var configuration = OptionsPage.ButtonsConfiguration
                          ?? ButtonsConfiguration.GetDefaultButtonsList(OptionsPage.ApplicationObject);

      _availableButtonsListBox.DisplayMember = "Caption";

      foreach (TaskbarButton button in configuration.AvailableButtons)
      {
        _availableButtonsListBox.Items.Add(button);
      }

      _taskbarButtonsListBox.DisplayMember = "Caption";

      foreach (TaskbarButton button in configuration.TaskbarButtons)
      {
        _taskbarButtonsListBox.Items.Add(button);
      }
    }


    private void TaskbarButtonsListBoxKeyDown(object sender, KeyEventArgs e)
    {
      var selectedButton = SelectedButton;

      if (selectedButton == null || !_taskbarButtonsListBox.Items.Contains(SelectedButton))
      {
        return;
      }

      if (e.KeyCode == Keys.Left)
      {
        _taskbarButtonsListBox.Items.Remove(selectedButton);
        _availableButtonsListBox.Items.Add(selectedButton);
      }

      var selectedindex = _taskbarButtonsListBox.Items.IndexOf(selectedButton);

      if (e.KeyCode == Keys.Up && selectedindex != 0)
      {
        selectedindex--;
        _taskbarButtonsListBox.Items.Remove(selectedButton);
        _taskbarButtonsListBox.Items.Insert(selectedindex, selectedButton);
        _taskbarButtonsListBox.SelectedIndex = selectedindex + 1;
      }

      if (e.KeyCode == Keys.Down && selectedindex != _taskbarButtonsListBox.Items.Count - 1)
      {
        selectedindex++;
        _taskbarButtonsListBox.Items.Remove(selectedButton);
        _taskbarButtonsListBox.Items.Insert(selectedindex, selectedButton);
        _taskbarButtonsListBox.SelectedIndex = selectedindex - 1;
      }
    }


    private void TaskbarButtonsListBoxSelectedIndexChanged(object sender, EventArgs e)
    {
      _availableButtonsListBox.SelectedIndexChanged -= AvailableButtonsListBoxSelectedIndexChanged;
      _availableButtonsListBox.ClearSelected();
      _availableButtonsListBox.SelectedIndexChanged += AvailableButtonsListBoxSelectedIndexChanged;

      SelectedButton = (TaskbarButton)_taskbarButtonsListBox.SelectedItem;
    }


    private void UpdateButtonClick(object sender, EventArgs e)
    {
      if (SelectedButton == null)
      {
        return;
      }

      var commandBarButton = OptionsPage.ApplicationObject.GetCommandBarButton(_menuItemPathTextBox.Text);

      if (commandBarButton == null)
      {
        MessageBoxHelpers.ShowError(Handle, "Menu item not found.", "Error");
      }
      else
      {
        SelectedButton.HideWhenDisabled = _hideWhenDisabledCheckBox.Checked;
        SelectedButton.MenuItemPath = _menuItemPathTextBox.Text;
        SelectedButton.Initialize(OptionsPage.ApplicationObject);

        UpdateListItems(_availableButtonsListBox);
        UpdateListItems(_taskbarButtonsListBox);
      }
    }


    private void UpdateButtonOptionsControls()
    {
      if (SelectedButton != null)
      {
        _hideWhenDisabledCheckBox.Checked = SelectedButton.HideWhenDisabled;
        _menuItemPathTextBox.Text = SelectedButton.MenuItemPath;
        _updateButton.Enabled = true;
      }
      else
      {
        _updateButton.Enabled = false;
      }
    }

    #endregion
  }
}